package org.lgiavedoni.android.franq.core.listener;

import org.lgiavedoni.android.franq.service.config.ConfigService;
import org.lgiavedoni.android.franq.service.config.ConfigService.ConfigKey;
import org.lgiavedoni.android.franq.util.collection.CollectionUtils;
import org.lgiavedoni.android.franq.util.thread.ThreadUtil;

import android.content.Context;
import android.content.Intent;

public abstract class PhoneBroadcastUserSafeListener extends PhoneBroadcastListener {

	@Override
	public final synchronized void onReceive(Context arg0, Intent intent) {
		unregisterListener();

		_onReceive(arg0, intent);

		new Register().start();
	}

	private class Register extends Thread {
		@Override
		public void run() {
			setName("Sensor Register [" + CollectionUtils.toString(getListenetFilter().actionsIterator(), ",") + ", Wakeup: "
					+ ThreadUtil.wakeUp(ConfigService.inst.getValueAsLong(ConfigKey.SENSOR_LISTENER_DISABLE_PERIOD)) + "]");
			ThreadUtil.sleep(ConfigService.inst.getValueAsLong(ConfigKey.SENSOR_LISTENER_DISABLE_PERIOD));
			registerListener();
		}

	}

	public abstract void _onReceive(Context arg0, Intent intent);

}
